package com.tang.base;


import com.tang.util.MD5Util;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * CreateTime: 2016-04-26下午1:06
 * Author: wjkjinke00@126.com
 * Description:
 */
public abstract class MapParamsRequest extends BaseRequest {
    protected TreeMap<String, Object> params;

    public MapParamsRequest() {
        this.params = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
    }

    public Map<String, Object> params() {
        params.clear();
        putParams();
//        params.put("flagtime", System.currentTimeMillis());
//        params.put("sig", sig());
        return params;
    }

    private String sig() {
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, Object>> entrySet = params.entrySet();
        for (Map.Entry<String, Object> stringObjectEntry : entrySet) {
            String key = stringObjectEntry.getKey();
            Object value = stringObjectEntry.getValue();
            sb.append(key);
            sb.append("=");
            sb.append(value + "");
            sb.append("&");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("sigkey123456");
        String md5 = MD5Util.getMD5(sb.toString());
        return md5;
    }

    protected abstract void putParams();
}
